//
//  Solution.cpp
//  labuladong
//
//  Created by 刘振舟 on 2023/5/19.
//

#include "ArraySolution.hpp"


int Solution::removeDuplicates(vector<int> &nums) {
    int slow = 0, fast = 0;
    while (fast < nums.size()) {
        if (nums[slow] != nums[fast]) {
            nums[++slow] = nums[fast];
        }
        ++fast;
    }
    return slow + 1;
}

int Solution::removeElement(vector<int> &nums, int val) {
    int slow = 0, fast = 0;
    while (fast < nums.size()) {
        if (nums[fast] != val) {
            nums[slow++] = nums[fast];
        }
        ++fast;
    }
    return slow;
}

void Solution::moveZeroes(vector<int> &nums) {
    int slow = 0, fast = 0;
    while (slow < nums.size()) {
        if (fast < nums.size()) {
            if (nums[fast] != 0) {
                nums[slow++] = nums[fast];
            }
            ++fast;
        } else {
            nums[slow++] = 0;
        }
    }
}

vector<int> Solution::twoSum(vector<int> &numbers, int target) {
    int left = 0, right = numbers.size() - 1;
    while (left < right) {
        int sum = numbers[left] + numbers[right];
        if (sum == target) {
            return {left + 1, right + 1};
        } else if (sum > target) {
            --right;
        } else {
            ++left;
        }
    }
    return {-1, -1};
}

void Solution::reverseString(vector<char> &s) { 
    reverse(s.begin(), s.end());
}

std::string getPalindrome(std::string &str, int left, int right) {
    while (left >= 0 && right < str.length() && str[left] == str[right]) {
        --left;
        ++right;
    }
    return str.substr(left + 1, (right - left - 1));
}

std::string Solution::longestPalindrome(std::string s) { 
    string res;
    for (int i = 0; i < s.length(); ++i) {
        std::string lStr = getPalindrome(s, i, i);
        std::string rStr = getPalindrome(s, i, i + 1);
        res = lStr.length() > res.length() ? lStr : res;
        res = rStr.length() > res.length() ? rStr : res;
    }
    return res;
}





